/**
 * @file drv_usart.h
 * @brief 
 * 
 * @author dalin (dalin@robot.com)
 * @version 1.0
 * @date 2023-05-08
 * 
 * @copyright Copyright (c) 2023  OpenSir Tech.co, Ltd
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2023-05-08 <td>1.0     <td>dalin     <td>内容
 * </table>
 */

#ifndef DRV_USART_H_
#define DRV_USART_H_

#include "drv_config.h"

#ifdef USE_USART_DRIVER
#include "usart.h"

#if 1   /* printf */
#include "stdio.h"
#ifdef __GNUC__
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
#endif  /* printf */


/* 定义接收DMA缓存大小，根据实际修改 */
#define DMA_IDLE_LEN    128

/* 打印的串口号 */
#if (PRINTF_PORT == 0x01)
#define PRINTF_API 		&huart1
#elif (PRINTF_PORT == 0x02)
#define PRINTF_API 		&huart2
#elif (PRINTF_PORT == 0x03)
#define PRINTF_API 		&huart3
#elif (PRINTF_PORT == 0x04)
#define PRINTF_API 		&huart4
#elif (PRINTF_PORT == 0x05)
#define PRINTF_API 		&huart5
#elif (PRINTF_PORT == 0x06)
#define PRINTF_API 		&huart6
#endif

typedef enum USARTNUMBER_
{
    DRV_UART_INIT = 0,
    DRV_UART1,
    DRV_UART2,
    DRV_UART3,
    DRV_UART4,
    DRV_UART5,
    DRV_UART6,

    DRV_UART_NUMBER_MAX
}usartNumber_e;

typedef struct 
{
    uint8_t flag;
    uint8_t buf[DMA_IDLE_LEN];
    uint16_t len;
}usartDmaIdle_t;


/*************************************** 阻塞 ***************************************/
int drv_usart_send(usartNumber_e usart, uint8_t * data, uint16_t len, uint32_t timeout);
int  drv_usart_recv(usartNumber_e usart, uint8_t * data, uint16_t len, uint32_t timeout);

/*************************************** 中断 ***************************************/
int drv_usart_send_it(UART_HandleTypeDef *usart, uint8_t *data, uint16_t len);
int drv_usart_recv_it(UART_HandleTypeDef *usart, uint8_t * data, uint16_t len);
int drv_usart_recv_it_enable(usartNumber_e usart, uint8_t * data, uint16_t len);

/* 接收回调函数 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart);

/*************************************** DMA ***************************************/
int drv_uart_dma_send(usartNumber_e uart, uint8_t *data, uint16_t len);
int drv_uart_dma_recv(usartNumber_e uart,uint8_t *data, uint16_t len);

/************************************ DMA 空闲中断 **********************************/
int uart_start_idle_recv(usartNumber_e uart, uint8_t * data);
int drv_uart_idle(usartNumber_e uart);
uint16_t usart_idle_get_data(usartNumber_e uart, uint8_t * data);

#endif /* USE_USART_DRIVER */
#endif /* DRV_USART_H_ */
